import os
from tkinter import *
from tkinter import filedialog, messagebox
from reportlab.lib.pagesizes import A4
from reportlab.lib import colors
from reportlab.pdfgen import canvas
from reportlab.lib.units import mm

# 生成桌牌PDF的函数
def create_name_card(c, name, font_name, font_size, font_color, bg_image, width, height, x, y):
    # 绘制背景图片
    if bg_image and os.path.exists(bg_image):
        c.drawImage(bg_image, x, y, width=width, height=height)

    # 设置字体和颜色
    c.setFont(font_name, font_size)
    c.setFillColor(font_color)

    # 计算文本位置（居中）
    text_width = c.stringWidth(name, font_name, font_size)
    text_x = x + (width - text_width) / 2
    text_y = y + (height - font_size) / 2

    # 绘制名字
    c.drawString(text_x, text_y, name)

def generate_pdf(output_filename, names, font_name, font_size, font_color, bg_image, card_width, card_height):
    c = canvas.Canvas(output_filename, pagesize=A4)
    width, height = A4

    # 计算边距和间距
    margin = 10 * mm
    spacing = 5 * mm

    # 计算每页可以放置多少个桌牌
    cards_per_row = int((width - 2 * margin) // (card_width + spacing))
    cards_per_col = int((height - 2 * margin) // (card_height + spacing))
    cards_per_page = cards_per_row * cards_per_col

    for i, name in enumerate(names):
        if i > 0 and i % cards_per_page == 0:
            c.showPage()  # 开始新的一页

        # 计算位置
        row = (i % cards_per_page) // cards_per_row
        col = (i % cards_per_page) % cards_per_row
        x = margin + col * (card_width + spacing)
        y = height - margin - (row + 1) * (card_height + spacing)

        # 创建桌牌
        create_name_card(c, name, font_name, font_size, font_color, bg_image, card_width, card_height, x, y)

    c.save()
    messagebox.showinfo("成功", f"PDF已生成: {output_filename}")

# GUI界面
class NameCardApp:
    def __init__(self, root):
        self.root = root
        self.root.title("桌牌生成器")
        self.root.geometry("400x400")

        # 名字列表
        Label(root, text="名字列表（每行一个）:").pack()
        self.names_text = Text(root, height=10, width=40)
        self.names_text.pack()

        # 字体名称
        Label(root, text="字体名称:").pack()
        self.font_name = Entry(root)
        self.font_name.insert(0, "Helvetica")
        self.font_name.pack()

        # 字体大小
        Label(root, text="字体大小:").pack()
        self.font_size = Entry(root)
        self.font_size.insert(0, "24")
        self.font_size.pack()

        # 字体颜色
        Label(root, text="字体颜色（例如 black, red, #0000FF）:").pack()
        self.font_color = Entry(root)
        self.font_color.insert(0, "black")
        self.font_color.pack()

        # 背景图片
        Label(root, text="背景图片路径:").pack()
        self.bg_image = Entry(root)
        self.bg_image.pack()
        Button(root, text="选择图片", command=self.select_image).pack()

        # 桌牌尺寸
        Label(root, text="桌牌宽度 (mm):").pack()
        self.card_width = Entry(root)
        self.card_width.insert(0, "90")
        self.card_width.pack()

        Label(root, text="桌牌高度 (mm):").pack()
        self.card_height = Entry(root)
        self.card_height.insert(0, "55")
        self.card_height.pack()

        # 生成按钮
        Button(root, text="生成PDF", command=self.generate).pack()

    def select_image(self):
        file_path = filedialog.askopenfilename(filetypes=[("图片文件", "*.jpg;*.png;*.jpeg")])
        if file_path:
            self.bg_image.delete(0, END)
            self.bg_image.insert(0, file_path)

    def generate(self):
        # 获取输入
        names = self.names_text.get("1.0", END).strip().split("\n")
        font_name = self.font_name.get()
        font_size = int(self.font_size.get())
        font_color = self.font_color.get()
        bg_image = self.bg_image.get()
        card_width = float(self.card_width.get()) * mm
        card_height = float(self.card_height.get()) * mm

        # 检查输入
        if not names:
            messagebox.showerror("错误", "请输入至少一个名字")
            return

        # 选择输出文件路径
        output_filename = filedialog.asksaveasfilename(defaultextension=".pdf", filetypes=[("PDF文件", "*.pdf")])
        if not output_filename:
            return

        # 生成PDF
        generate_pdf(output_filename, names, font_name, font_size, font_color, bg_image, card_width, card_height)

# 运行程序
if __name__ == "__main__":
    root = Tk()
    app = NameCardApp(root)
    root.mainloop()